import scipy.io as sio
import cv2
import numpy as np
import h5py
import collections

def test():
    f_path = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/Data-USPS/USPS.mat'
    data=sio.loadmat(f_path)
    print data['fea'][1].reshape(1,16,16)
    print data['gnd'][:,0]

    """
    print dict(collections.Counter(data['gnd'][0:7291][:,0]))

    cv2.imshow('result',np.asarray(data['fea'][1].reshape(1,16,16)))
    cv2.waitKey(0)
"""
def transform_to_keras_type():
    f_path = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/Data-USPS/USPS.mat'
    data=sio.loadmat(f_path)
    trainVec_path = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/Data-USPS/trainVec.h5'
    testVec_path = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/Data-USPS/testVec.h5'

    train_imgs = []
    train_labels = []

    test_imgs = []
    test_labels = []

    for i in range(7291):
        if data['gnd'][i] == 10:
            data['gnd'][i] = 0
        train_imgs.append(data['fea'][i].reshape(1,16,16))
        train_labels.append(data['gnd'][i])
    train_img_arrs = np.asarray(train_imgs,dtype='float32')
    train_labels_arrs = np.asarray(train_labels,dtype='int32')
    for j in xrange(9298-7291):
        j = j + 7291
        if data['gnd'][j] == 10:
            data['gnd'][j] = 0
        test_imgs.append(data['fea'][j].reshape(1,16,16))
        test_labels.append(data['gnd'][j])
    test_img_arrs = np.asarray(test_imgs,dtype='float32')
    test_labels_arrs = np.asarray(test_labels,dtype='int32')

    f_train = h5py.File(trainVec_path,'w')
    f_train.create_dataset('x',data=train_img_arrs)
    f_train.create_dataset('y',data=train_labels_arrs)
    f_train.close()

    f_test = h5py.File(testVec_path,'w')
    f_test.create_dataset('x',data=test_img_arrs)
    f_test.create_dataset('y',data=test_labels_arrs)
    f_test.close()

def load_USPS_data():
    train_file_name = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/Data-USPS/trainVec.h5'
    test_file_name = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/Data-USPS/testVec.h5'
    def load_data_xy(file_name):
        datas  = []
        labels = []
        f = h5py.File(file_name, 'r')
        x = f['x'][:]
        y = f['y'][:]
        datas.append(x)
        labels.append(y)
        combine_d = np.vstack(datas)
        combine_l = np.hstack(labels)
        return combine_d, combine_l

    train_set_x, train_set_y = load_data_xy(train_file_name)
    valid_set_x, valid_set_y = load_data_xy(test_file_name)
    return [(train_set_x, train_set_y), (valid_set_x, valid_set_y)]

if __name__ == "__main__":
    transform_to_keras_type()